<?php
namespace Admin\Controller;
use Think\Controller;
class ProblemController extends Controller {
   public $ary = array();
    public function index(){
        $pd = "内容设置";
        $pt = "分类管理";
        $this->assign('pd', $pd);
        $this->assign('pt', $pt);
//        echo "dsdsssd";
    }
    public function problemclass(){
        $pd = "内容设置";
        $pt = "分类管理";
        $this->assign('pd', $pd);
        $this->assign('pt', $pt);
        $m = M();
        $sql = "select * from wentifz";
        $fz = $m->query($sql);
        var_dump($this->_arry($fz,0));
        $fz = $this->ary;
        $sfz = array();
        foreach($fz as $v){
            if($v['fid']=="0"){
                $v['ftext'] = '无父类';
                $sfz[] = $v;
            }else{
                $sql = "select text from wentifz where id = {$v['fid']}";
                $ret = $m->query($sql);
                $v['ftext'] = $ret[0]['text'];
                $sfz[] = $v;
            }
        }
        $fz = $sfz;
        $this->assign("fz",$fz);
        $this->display("problemclass");
    }
    public function classadd(){
        if(IS_POST){
            $fid = I("post.fid");
            $text = I("post.text");
            $m = M();
            $sql = "insert into wentifz (fid,text) values({$fid},'{$text}')";
            $ret = $m->execute($sql);
            if($ret = 0){
                $this->error("添加失败",U(''));
            }else{
                $this->error("添加成功",U(''));
            }
        }else{
            $m = M();
            $sql = "select * from wentifz";
            $fz = $m->query($sql);
            var_dump($this->_arry($fz,0));
            $fz = $this->ary;
            $this->assign("fz",$fz);
            $this->display("classadd");
            $arr = array();
//            var_dump( $fz);
        }
    }
    private function _arry($fz,$fid,$cs=0){
        $cs++;
        foreach($fz as $k => $v){
            if($v['fid'] == $fid) {
                $cc = "";
                for($i=0;$i<=$cs-2;$i++){
                    $cc = $cc."--";
                }
                $v['text']=$cc.$v['text'];
                $this ->ary[] = $v;
//                unset($fz[$k]);
                $this ->_arry($fz,$v['id'],$cs);
            }
        }
        return;
    }

}